package com.peanut.baby.mvp;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.peanut.devlibrary.BaseActivity;

/**
 * Created by anonymouslystudio on 2017/6/15.
 */

public abstract class  BaseMvpFragment extends Fragment {
    protected boolean inited = false;
    protected boolean isActive(){
        return isAdded();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    public boolean isInited(){
        return inited;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    protected void showToast(final String s){
        if(getActivity() != null){
            Toast.makeText(getActivity(),s,Toast.LENGTH_SHORT).show();
        }
    }

    protected void showProgress(final String msg,final boolean cancelable){
        if(getActivity() != null){
            if(getActivity() instanceof BaseActivity){
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((BaseActivity) getActivity()).showProgressDialog(msg,cancelable);
                    }
                });

            }
        }
    }

    protected  void dismissProgress(){
        if(getActivity() != null){
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(getActivity() instanceof BaseActivity){
                        ((BaseActivity) getActivity()).dismissProgressDialog();
                    }
                }
            });

        }
    }


    public abstract void initData();
}
